﻿using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.SheepCommands.EwesGiveBirthes
{
    public class EwesGiveBirthBackFillHandler : IRequestHandler<EwesGiveBirthBackFillCommand, Result>
    {
        private readonly IBaseRepository<EwesGiveBirth> _ewesGiveBirthRepository;

        public EwesGiveBirthBackFillHandler(IBaseRepository<EwesGiveBirth> ewesGiveBirthRepository)
        {
            _ewesGiveBirthRepository = ewesGiveBirthRepository;
        }

        /// <summary>
        /// 种母分娩反填业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(EwesGiveBirthBackFillCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Deleted };
            try
            {
                var ewesGiveBirth = await (await _ewesGiveBirthRepository.GetAll()).Where(c => c.EwesGiveBirthId == request.EwesGiveBirthId).FirstOrDefaultAsync();
                if (ewesGiveBirth != null)
                {
                    result.Data = ewesGiveBirth;
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
